def min_index(array):
    m = array[0]
    j = 0
    for i, v in enumerate(array):
        if v < m:
            m = v
            j = i
    return j, m


def max_index(array):
    m = array[0]
    j = 0
    for i, v in enumerate(array):
        if v > m:
            m = v
            j = i
    return j, m


def sort_select(array, is_asc):
    result = []
    while len(array) > 0:  #
        if is_asc:
            j, v = min_index(array)  # 计算最小值
        else:
            j, v = max_index(array)  # 计算最小值
        result.append(v)
        array.pop(j)
    return result


# 冒泡排序
def bubble_sort(array, is_asc = True):
    i = 0
    while i < len(array):
        j = 0
        while j < len(array) - i - 1:
            if is_asc:
                if array[j] > array[j + 1]:
                    array[j], array[j + 1] = array[j + 1], array[j]
            else:
                if array[j] < array[j + 1]:
                    array[j], array[j + 1] = array[j + 1], array[j]
            j += 1
        i += 1
    return array


# 选择better
def sort_select_better(array, is_asc=True):
    i = 0
    while i < len(array):
        start = i
        m = array[start]
        j = start
        while start < len(array):
            if is_asc:
                if array[start] < m:
                    m = array[start]
                    j = start
            else:
                if array[start] > m:
                    m = array[start]
                    j = start
            start += 1
        array[j], array[i] = array[i], array[j]
        i += 1
    return array


if __name__ == '__main__':
    nums = [5, 3, 7, 4, 6, 2, 1]
    print(bubble_sort(nums, False))

